import java.util.Scanner;

/**
 * 
 */

/**
 * @author HOANB
 * 
 */
public class MainClass {
	static boolean isName = false;
	private static Scanner input;
	static boolean isContinue = false;
	static boolean isCount = false;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		input = new Scanner(System.in);
		String strName = "";
		String[] strFirstName = null;
		String[] strLastName = null;
		String str = "";
		String str2 = "";
		int count = 0;
		int countStudent = -1;
		do {
			System.out.println("Enter count of students: ");
			try {
				count = Integer.parseInt(input.nextLine());
				isCount = true;
				strFirstName = new String[count];
				strLastName = new String[count];
			} catch (Exception ex) {
				isCount = false;
				System.out
						.println("Count of student invalid!Please try again!");
			}
		} while (!isCount);

		do {
			if (countStudent == count - 1) {
				break;
			}
			System.out.println("Enter your name(firtname lastname)): ");
			strName = input.nextLine();
			String[] nameArray = strName.split(" ");
			if (nameArray.length < 2) {
				isName = false;
				System.out.println("Your name invalid!Please try again!");
			}
			if (nameArray.length >= 2) {
				countStudent++;
				String strTrimName = strName.trim();
				String[] arrayStr = strTrimName.trim().split(" ");
				int index = 0;

				nameArray = new String[strTrimName.length()];
				for (int i = 0; i < arrayStr.length; i++) {
					if (!arrayStr[i].isEmpty()) {
						nameArray[index] = arrayStr[i];
						index++;
					}
				}

				str = "";
				str2 = "";
				for (int i = 0; i < index; i++) {
					if (i + 1 < index) {

						str = str + nameArray[i] + " ";
					}
					if (i + 1 == index) {
						str2 = nameArray[i];
					}
				}
				strFirstName[countStudent] = str;
				System.out.println("TT: " + strFirstName[countStudent]);
				strLastName[countStudent] = str2;
				System.out.println("GG: " + strLastName[countStudent]);
			}
			if (countStudent == count - 1) {
				break;
			}
			do {
				System.out.println("Continue? Y/N");
				String strContinue = input.nextLine();
				if (strContinue.equalsIgnoreCase("N")) {
					isName = true;
					break;
				}
				if (strContinue.equalsIgnoreCase("Y")) {
					isContinue = true;
				} else {
					isContinue = false;
					System.out.println("Y Or N.Please try again!");
				}
			} while (!isContinue);
		} while (!isName);

		// strFirstName = new String[countStudent];
		// strLastName = new String[countStudent];

		for (int i = 0; i < countStudent; i++) {
			for (int j = 1; j < countStudent; j++) {
				if (strLastName[j].compareTo(strLastName[j - 1]) < 0) {
					String temp = strLastName[j];
					strLastName[j] = strLastName[j - 1];
					strLastName[j - 1] = temp;

					String temp2 = strFirstName[j];
					strFirstName[j] = strFirstName[j - 1];
					strFirstName[j - 1] = temp2;
				}

				if (strLastName[j].compareTo(strLastName[j - 1]) == 0) {
					if (strFirstName[j].compareTo(strFirstName[j - 1]) < 0) {
						String temp3 = strFirstName[j];
						strFirstName[j] = strFirstName[j - 1];
						strFirstName[j - 1] = temp3;
					}
				}
			}
		}
		for (int i = 0; i <= countStudent; i++) {
			System.out.println(strFirstName[i] + strLastName[i]);
		}
	}
}
